// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/6iZiqQZBQJY

 fs = require('fs');
var data = fs.readFileSync('words.json');
var words = JSON.parse(data);
var acoounts =fs.readFileSync('acoounts.json');
var IDs = JSON.parse(acoounts);
const bodyParser = require('body-parser');
const mongoose = require('mongoose')
mongoose.connect('mongodb://localhost/test1', { useNewUrlParser: true , useUnifiedTopology: true } )
mongoose.Promise= global.Promise;

// var http = require('http');
// var https = require('https');
// var privateKey  = fs.readFileSync('sslcert/server.key', 'utf8');
// var certificate = fs.readFileSync('sslcert/server.cert', 'utf8');

// var credentials = {key: privateKey, cert: certificate};
var express = require('express');

// const { nextTick } = require('process');

 const app = express();
//  var httpServer = http.createServer(app);
//  var httpsServer = https.createServer( credentials, app);
// const quespage = require("./website/questiongame/quespage");


app.use(express.json());



app.use('/',require('./routes/api'))
app.use('/',require('./routes/userRoute'))
app.use('/',require('./routes/multiRoutes'))
app.use('/',require('./routes/arbitrary'))
app.use('/',require('./routes/myactivities'))
var useraccessID = 1
;



app.get('/all', sendAll);

function sendAll(request, response) {
  response.send(words);
}

app.get('/allid', sendAllID);

function sendAllID(request, response) {
  response.send(IDs);
}

app.get('/search/:word/', searchWord);

function searchWord(request, response) {
  var word = request.params.word;
  var reply;
  if (words[word]) {
    reply = {
      status: 'found',
      word: word,
      score: words[word]
    };
  } else {
    reply = {
      status: 'not found',
      word: word
    };
  }
  response.send(reply);
}

app.get('searchid/:id/', searchID);

function searchID(request, response) {
  var id = request.params.id;
  var reply;
  if (IDs[id]) {
    reply = {
      status: 'found',
      ID: id,
      时间: IDs[id]
    };
  } else {
    reply = {
      status: 'not found',
      ID: id
    };
  }
  response.send(reply);
}

app.use(express.static('website'))
// app.listen(3000);
// httpServer.listen(3000);
// httpsServer.listen(3001);
app.listen(3000)
app.listen(3001)
console.log('')
// var io = require('socket.io')(server);

// // Register a callback function to run when we have an individual connection
// // This is run for each individual user that connects
// io.sockets.on('connection',
//   // We are given a websocket object in our function
//   function (socket) {
  
//     console.log("We have a new client: " + socket.id);
  
//     // When this user emits, client side: socket.emit('otherevent',some data);
//     socket.on('mouse',
//       function(data) {
//         // Data comes in as whatever was sent, including objects
//         console.log("Received: 'mouse' " + data.x + " " + data.y);
      
//         // Send it to all other clients
//         socket.broadcast.emit('mouse', data);
        
//         // This is a way to send to everyone including sender
//         // io.sockets.emit('message', "this goes to everyone");

//       }
//     );
    
//     socket.on('disconnect', function() {
//       console.log("Client has disconnected");
//     });
//   }
// );